package com.ee.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ee.bean.Equipment;
import com.ee.bean.User;
import com.ee.bean.Workflow;
import com.ee.service.EquipmentService;
import com.ee.service.UserService;
import com.ee.service.WorkflowService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("workflowAction")
@Scope("prototype")
public class WorkflowAction extends ActionSupport  implements ServletRequestAware, ServletResponseAware{
	private Workflow workflow;
	private WorkflowService workflowService;
	private EquipmentService equipmentService;
	private UserService userService;
	private HttpServletRequest request;
	private HttpServletResponse response;
	/**
	 * 删除流程
	 * @return
	 */
	public String delete(){
		int id=Integer.parseInt((String) request.getParameter("id"));
		workflow = workflowService.findWorkflowById(id);
		workflowService.delete(workflow);
		return "_list";
	}
	/**
	 * 操作人员个人删除
	 * @return
	 */
	public String perDelete(){
		int id=Integer.parseInt((String) request.getParameter("id"));
		workflow = workflowService.findWorkflowById(id);
		workflowService.delete(workflow);
		return "_perList";
	}
	/**
	 * 添加流程
	 * @return
	 * @throws ParseException
	 */
	public String add() throws ParseException{
		int id=Integer.parseInt((String) request.getParameter("equipmentid"));
		Equipment equipment = equipmentService.getEquipmentById(id);
		workflow.setEquipment(equipment);
		int userid = Integer.parseInt((String) request.getParameter("userid"));
		User user = userService.getUserById(userid);
		workflow.setUser(user);
		String time = request.getParameter("time");
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = simpleDateFormat.parse(time);
		workflow.setTime(date);
		if("".equals(this.workflow.getId()) || this.workflow.getId() == 0){
			workflowService.add(workflow);
		}else{
			workflowService.update(workflow);
		}
		return "_list";
	}
	/**
	 * 操作人员添加流程
	 * @return
	 * @throws ParseException
	 */
	public String perAdd() throws ParseException{
		int id=Integer.parseInt((String) request.getParameter("equipmentid"));
		Equipment equipment = equipmentService.getEquipmentById(id);
		workflow.setEquipment(equipment);
		User currentUser = (User)request.getSession().getAttribute("user");
		workflow.setUser(currentUser);
		String time = request.getParameter("time");
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = simpleDateFormat.parse(time);
		workflow.setTime(date);
		if("".equals(this.workflow.getId()) || this.workflow.getId() == 0){
			workflowService.add(workflow);
		}else{
			workflowService.update(workflow);
		}
		return "_perList";
	}
	/**
	 * admin的工作流列表
	 * @return
	 */
	public String list(){
		Map request = (Map)ActionContext.getContext().get("request");
		request.put("list",workflowService.findAllWorkflows());
		request.put("user",userService.findOperators());
		request.put("equipment",equipmentService.getAllEquipments());
		return "list";
	}
	/**
	 * 个人的工作流列表
	 * @return
	 */
	public String perList(){
		User currentUser = (User)request.getSession().getAttribute("user");
		Map request = (Map)ActionContext.getContext().get("request");
		request.put("list",workflowService.findWorkflowsByUser(currentUser));
		request.put("equipment",equipmentService.getAllEquipments());		
		return "perList";
	}
	public Workflow getWorkflow() {
		return workflow;
	}
	public void setWorkflow(Workflow workflow) {
		this.workflow = workflow;
	}
	public WorkflowService getWorkflowService() {
		return workflowService;
	}
	
	@Resource
	public void setWorkflowService(WorkflowService workflowService) {
		this.workflowService = workflowService;
	}
	
	public EquipmentService getEquipmentService() {
		return equipmentService;
	}
	
	@Resource
	public void setEquipmentService(EquipmentService equipmentService) {
		this.equipmentService = equipmentService;
	}

	public UserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;		
	}
	
}
